package com.pzb.market.service.impl;

import java.util.List;

import com.pzb.page.domain.PgPageItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.market.mapper.MkGroupbuyingGoodsMapper;
import com.pzb.market.domain.MkGroupbuyingGoods;
import com.pzb.market.service.IMkGroupbuyingGoodsService;

/**
 * 拼购商品Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-12-22
 */
@Service
public class MkGroupbuyingGoodsServiceImpl implements IMkGroupbuyingGoodsService 
{
    @Autowired
    private MkGroupbuyingGoodsMapper mkGroupbuyingGoodsMapper;

    /**
     * 查询拼购商品列表
     * 
     * @param mkGroupbuyingGoods 拼购商品
     * @return 拼购商品
     */
    @Override
    public List<MkGroupbuyingGoods> selectMkGroupbuyingGoodsList(MkGroupbuyingGoods mkGroupbuyingGoods)
    {
        return mkGroupbuyingGoodsMapper.selectMkGroupbuyingGoodsList(mkGroupbuyingGoods);
    }

    /**
     * 新增拼购商品
     * 
     * @param groupbuyGoodsList 拼购商品
     * @return 结果
     */
    @Override
    public int insertMkGroupbuyingGoods(List<MkGroupbuyingGoods> groupbuyGoodsList)
    {
        int count = 0;
        //查询最后排序号
        Integer sort = mkGroupbuyingGoodsMapper.selectLastSort(groupbuyGoodsList.get(0).getGroupId());
        if(sort ==null)sort=0;
        for (MkGroupbuyingGoods mkGroupbuyingGoods : groupbuyGoodsList) {
            //查找有无该商品
            int has = mkGroupbuyingGoodsMapper.checkExist(mkGroupbuyingGoods);
            if(has == 0){
                sort++;
                mkGroupbuyingGoods.setSort(sort);
                mkGroupbuyingGoodsMapper.insertMkGroupbuyingGoods(mkGroupbuyingGoods);
                count++;
            }
        }
        return count;
    }



    /**
     * 查询拼购商品
     *
     * @param id 拼购商品主键
     * @return 拼购商品
     */
    @Override
    public MkGroupbuyingGoods selectMkGroupbuyingGoodsById(Long id)
    {
        return mkGroupbuyingGoodsMapper.selectMkGroupbuyingGoodsById(id);
    }

    /**
     * 修改拼购商品
     * 
     * @param mkGroupbuyingGoods 拼购商品
     * @return 结果
     */
    @Override
    public int updateMkGroupbuyingGoods(MkGroupbuyingGoods mkGroupbuyingGoods)
    {
        return mkGroupbuyingGoodsMapper.updateMkGroupbuyingGoods(mkGroupbuyingGoods);
    }

    /**
     * 批量删除拼购商品
     * 
     * @param ids 需要删除的拼购商品主键
     * @return 结果
     */
    @Override
    public int deleteMkGroupbuyingGoodsByIds(Long[] ids)
    {
        MkGroupbuyingGoods goods = selectMkGroupbuyingGoodsById(ids[0]);
        int count = mkGroupbuyingGoodsMapper.deleteMkGroupbuyingGoodsByIds(ids);
        //对删除后的数据重新排序
        MkGroupbuyingGoods q = new MkGroupbuyingGoods();
        q.setGroupId(goods.getGroupId());
        List<MkGroupbuyingGoods> groupGoodsList = selectMkGroupbuyingGoodsList(q);
        int sort = 1;
        for (MkGroupbuyingGoods mkGroupbuyingGoods : groupGoodsList) {
            mkGroupbuyingGoods.setSort(sort);
            updateMkGroupbuyingGoods(mkGroupbuyingGoods);
            sort++;
        }
        return count;
    }

    @Override
    public int moveUpGoods(MkGroupbuyingGoods mkGroupbuyingGoods) {
        int count=0;
        //查询上一个
        MkGroupbuyingGoods q = new MkGroupbuyingGoods();
        q.setGroupId(mkGroupbuyingGoods.getGroupId());
        q.setSort(mkGroupbuyingGoods.getSort()-1);
        List<MkGroupbuyingGoods> goodsList=mkGroupbuyingGoodsMapper.selectMkGroupbuyingGoodsList(q);
        if (goodsList.size()>0) {
            mkGroupbuyingGoods.setSort(mkGroupbuyingGoods.getSort()-1);
            count = mkGroupbuyingGoodsMapper.updateMkGroupbuyingGoods(mkGroupbuyingGoods);
            MkGroupbuyingGoods goods =goodsList.get(0);
            goods.setSort(goods.getSort()+1);
            mkGroupbuyingGoodsMapper.updateMkGroupbuyingGoods(goods);
        }
        return count;
    }

    @Override
    public int moveDownGoods(MkGroupbuyingGoods mkGroupbuyingGoods) {
        int count=0;
        //查询上一个
        MkGroupbuyingGoods q = new MkGroupbuyingGoods();
        q.setGroupId(mkGroupbuyingGoods.getGroupId());
        q.setSort(mkGroupbuyingGoods.getSort()+1);
        List<MkGroupbuyingGoods> goodsList=mkGroupbuyingGoodsMapper.selectMkGroupbuyingGoodsList(q);
        if (goodsList.size()>0) {
            mkGroupbuyingGoods.setSort(mkGroupbuyingGoods.getSort()+1);
            count = mkGroupbuyingGoodsMapper.updateMkGroupbuyingGoods(mkGroupbuyingGoods);
            MkGroupbuyingGoods goods =goodsList.get(0);
            goods.setSort(goods.getSort()-1);
            mkGroupbuyingGoodsMapper.updateMkGroupbuyingGoods(goods);
        }
        return count;
    }

}
